//
//  NSString+Verify.m
//  WC
//
//  Created by 杨鑫 on 15/4/28.
//  Copyright (c) 2015年 智康. All rights reserved.
//

#import <CommonCrypto/CommonDigest.h>
#import "NSString+Utilities.h"
//#import "Macros.h"

static NSArray *chNumbers;

@import UIKit;

@implementation NSString (Utilities)

+ (void)load
{
    chNumbers = @[@"一", @"二", @"三", @"四", @"五"];
}

+ (CGSize)sizeInFrame:(CGSize)frame attributedString:(NSAttributedString *)attrStr
{
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(frame.width, CGFLOAT_MAX)
                                        options:NSStringDrawingUsesLineFragmentOrigin
                                        context:nil];
    return rect.size;
}

+ (NSString *)distanceToString:(float)distance
{
    if (distance < 500) {
        return [NSString stringWithFormat:@"%.0fm", distance];
    } else {
        distance /= 1000.0;
        return [NSString stringWithFormat:@"%.1fkm", distance];
    }
}

+ (NSString *)radiusToChString:(int)radius
{
    int index = radius/1000;
    if (index > 5) {
        return [NSString stringWithFormat:@"%d公里内", index];
    } else {
        if (index == 0)
            index = 1;
        return [NSString stringWithFormat:@"%@公里内", chNumbers[index-1]];
    }
}

+ (NSString *)dataToJsonString:(id)data
{
    NSError *error = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:data
                                                       options:NSJSONWritingPrettyPrinted
                                                         error:&error];
    
    if ([jsonData length] > 0 && error == nil){
        return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }else{
        return nil;
    }
}

- (NSString *)md5String
{
    const char *string = self.UTF8String;
    int length = (int)strlen(string);
    unsigned char bytes[CC_MD5_DIGEST_LENGTH];
    CC_MD5(string, length, bytes);
    
    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
        [result appendFormat:@"%02x", bytes[i]];
    
    return result;
}

- (NSDate *)toDate:(NSString *)dateformatter
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    
    if (!dateformatter) {
        [formatter setDateFormat:@"yyyy-MM-dd"];
    } else {
        [formatter setDateFormat:dateformatter];
    }
    
    return [formatter dateFromString:self];
}

- (NSMutableAttributedString *)noInfoAttributedStringWithIcon:(NSString *)iconName insertPosition:(NSInteger)index
{
    NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
    pStyle.alignment = NSTextAlignmentCenter;
    pStyle.lineSpacing = 5;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self
                                                                                         attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16],
                                                                                                      NSForegroundColorAttributeName: kTxtColor,
                                                                                                      NSParagraphStyleAttributeName: pStyle}];
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:iconName];
    NSAttributedString *attrStringWithImage = [NSAttributedString attributedStringWithAttachment:textAttachment];
    [attributedString replaceCharactersInRange:NSMakeRange(index, 1) withAttributedString:attrStringWithImage];
    return attributedString;
}

- (NSString *)trim
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}

- (BOOL)hasSpace
{
    NSRange range = [self rangeOfString:@" "];
    if (range.location != NSNotFound) {
        return YES;
    }
    return NO;
}

- (BOOL)isEmpty
{
    return [@"" isEqualToString:self];
}

- (BOOL)isMobile
{

    NSString *regex = @"^1[3|4|5|7|8][0-9]{9}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}


- (BOOL)isPassword
{
    //密码长度6-16位
    NSUInteger length = self.length;
    if (length >=6 && length<=16) {
        return YES;
    } else {
        return NO;
    }
}

- (BOOL)isEmail
{
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

- (BOOL)isMoney
{
    NSString *regex = @"^([1-9]\\d{0,3}|0)(\\.\\d{0,2})?$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}
+ (NSString *)timeToString:(long long)time
{
    NSDate *date =[NSDate dateWithTimeIntervalSince1970:time /1000];
    NSDateFormatter *df =[[NSDateFormatter alloc]init];
    df.dateFormat =@"yyyy-MM-dd HH:mm:ss";
    NSString *timeStr =[df stringFromDate:date];
    return timeStr;
  
}
+ (NSString *)timeStringYYMMDDFrom:(long long)time{
    NSString * string =[ NSString timeToString:time];
    if (string.length <11) {
        return nil;
    }
    return [string substringToIndex:10];

}

+ (NSString *)timeStringMMDDHMFrom:(long long)time{
    NSDate *date =[NSDate dateWithTimeIntervalSince1970:time /1000];
    NSDateFormatter *df =[[NSDateFormatter alloc]init];
    df.dateFormat =@"MM-dd HH:mm";
    NSString *timeStr =[df stringFromDate:date];
    return timeStr;
}


- (BOOL )isInvalidValue{
    if(self ==nil ||[self isEqualToString:@""]){
        return YES;
    }
    return NO;
}

- (CGFloat)heightWithWidth:(CGFloat)width fontSize:(NSUInteger)font{

    CGFloat height =[self boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:font]} context:nil].size.height + 1;
    return height;

}

@end
